package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmUser;

import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.function.Consumer;

@Service
@Log4j2
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private  FastDFSClient fastDFSClient;
    @Autowired
   private WmMaterialMapper wmMaterialMapper;

    @Value("${fdfs.url}")
    private String fileServerUrl;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if (multipartFile == null)return errorParam;
        String s=null;
        try {
             s = fastDFSClient.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3.保存素材数据到表中 wm_material
        Integer userId = ThreadLocalUtils.getUserId();
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(s);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setUserId(userId);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        return ResponseResult.okResult(fileServerUrl+s);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
         if (dto==null)return errorParam;
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(WmMaterial::getUserId,ThreadLocalUtils.getUserId());
        queryWrapper.eq(dto.getIsCollection()!=null,WmMaterial::getIsCollection,dto.getIsCollection());
        ArrayList<WmMaterial> list = new ArrayList<>();
//        list.forEach(new Consumer<WmMaterial>() {
//            @Override
//            public void accept(WmMaterial wmMaterial) {
//                wmMaterial.setUrl(fileServerUrl+wmMaterial);
//            }
//        });
        return gePageDate(dto,queryWrapper);
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if(id==null){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial wmMaterial=getById(id);
        if (wmMaterial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(WmMaterial::getId,id);
        Integer count=wmMaterialMapper.selectCount(lambdaQueryWrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"当前图片被引用");

        }
        String fileId=wmMaterial.getUrl().replace(fileServerUrl,"");
        try {

        }catch (Exception e){
            e.printStackTrace();
            return  ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateStatus(Integer id, Short type) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer uid=WmThreadLocalUtils.getUser().getId();
        update(Wrappers.<WmMaterial>lambdaUpdate().set(WmMaterial::getIsCollection,type).eq(WmMaterial::getId,id).eq(WmMaterial::getUserId,uid));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}